﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Net;
using System.Text;

namespace NetworkServices
{
    public class NetworkServices
    {
        private static HttpWebRequest PrepareHttpRequest(string uri, string accept_encoding, string cookie, string referer, bool allowAutoRedirect)
        {
            ServicePointManager.Expect100Continue = false;
            // Prepare web request
            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(uri);
            myRequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
            //myRequest.Referer = "http://www.warez-bb.org/login.php";
            myRequest.ContentType = "application/x-www-form-urlencoded";
            myRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 2.0.50110;)";


            // Set the headers associated with the request.
            WebHeaderCollection myWebHeaderCollection = myRequest.Headers;
            myWebHeaderCollection.Add("Accept-Language: he-IL,en-US");//"Accept-Language: he"

            if (accept_encoding.Length > 0)
                myWebHeaderCollection.Add(accept_encoding);
            myWebHeaderCollection.Add("UA-CPU: x86");
            myWebHeaderCollection.Add("Cache-Control: no-cache");
            if (!String.IsNullOrEmpty(cookie))
                myWebHeaderCollection.Add(cookie);

            // Set some reasonable limits on resources used by this request
            myRequest.MaximumAutomaticRedirections = 1;
            myRequest.AllowAutoRedirect = allowAutoRedirect;
            myRequest.MaximumResponseHeadersLength = 100;
            if (!string.IsNullOrEmpty(referer))
                myRequest.Referer = referer;

            // Set credentials to use for this request.
            //myRequest.Credentials = CredentialCache.DefaultCredentials;

            return myRequest;
        }

        public static HttpWebRequest SendPostRequest(String uri,
                                                     String accept_encoding,
                                                     String cookie,
                                                     String post_data,
                                                     string referer)
        {
            // Prepare post data
            ASCIIEncoding encoding = new ASCIIEncoding();
            string postData = post_data;
            byte[] data = encoding.GetBytes(postData);

            HttpWebRequest req = PrepareHttpRequest(uri,
                                                    accept_encoding,
                                                    cookie, referer, true);
            req.Method = "POST";
            req.Timeout = 30000;
            req.ProtocolVersion = HttpVersion.Version11;

            req.ContentLength = data.Length;
            Stream newStream = req.GetRequestStream();
            // Send the data.
            newStream.Write(data, 0, data.Length);
            newStream.Flush();
            newStream.Close();

            return req;
        }

        public static WebRequest SendGetRequest(string uri, string accept_encoding, string cookie, string referer, bool allowAutoRedirect)
        {
            HttpWebRequest req = PrepareHttpRequest(uri,
                                                    accept_encoding,
                                                    cookie,
                                                    referer,
                                                    allowAutoRedirect);
            req.Method = "Get";
            req.Timeout = 30000;
            return req;
        }


        public static WebResponse SendGetRequest(string uri, bool useRangeAndAuthentication, string rsCreds)
        {
            WebRequest req = WebRequest.Create(uri);
            req.Timeout = 30000;
            req.ContentType = "application/x-www-form-urlencoded";
            req.Method = "Get";

            if (useRangeAndAuthentication)
            {
                ((HttpWebRequest)req).AddRange(0, 20);
                // user:password in base64 encoding
                string creds = Utils.Utils.EncodeTo64(rsCreds);
                req.Headers["Authorization"] = "Basic " + creds;
            }

            return GetResponse((HttpWebRequest)req);

        }

        private static WebResponse GetResponse(HttpWebRequest req)
        {
            WebResponse resp = null;

            try
            {
                resp = req.GetResponse();
            }
            catch (WebException e)
            {
                return null;
            }

            return resp;
        }


    }
}
